package com.example.cart_system.mapper;

import com.example.cart_system.pojo.Order;
import com.example.cart_system.pojo.Usr;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UsrMapper {
    @Select("select usrname from usr where usrname = #{usrName}")
    public String getUsrName(String name);
    //登录逻辑
    @Select("select usrid, usrname, usrcorn, usrplayer from usr where usrname = #{usrname} and usrpassword = MD5(#{usrpassword})")
    public Usr selectByUsrnameAndUsrpassword(Usr usr);
    //注册逻辑
    @Insert("insert into usr (usrid, usrname, usrpassword, usrcorn, usrplayer, createtime, logintime) values (#{usrid}, #{usrname}, MD5(#{usrpassword}), #{usrcorn}, #{usrplayer}, #{createTime}, #{loginTime})")
    public void insertUsr(Usr usr);
    //更新登录时间
    @Update("update usr set loginTime = #{loginTime} where usrid = #{usrId}")
    public void updateUsrLoginTime(String usrId, LocalDateTime loginTime);
    //用户购物车够买商品
    @Insert("insert into `order` (oid, goods, usrid, osumprice, otime) values (#{oid}, #{goods}, #{usrid}, #{osumprice}, #{otime})")
    public void buy(Order order);
    //得到排序好的用户姓名
    @Select("SELECT usr.usrname from `order`, usr WHERE `order`.usrid = usr.usrid GROUP BY `order`.usrid ORDER BY SUM(osumprice)")
    public List<Usr> selectAllUsrName();
}
